package gameObjects;

import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

/**
 * Classe che gestisce il conto alla rovescia sullo schermo di gioco
 * @author Massimiliano Palermo
 *
 */
public class ReverseCount extends Node{
	private static final long serialVersionUID = 18042010191046L;
	private Quad			panel;
	private int 			x1,y1,x2,y2;
	private DisplaySystem 	display = DisplaySystem.getDisplaySystem();
	private Renderer 		renderer = display.getRenderer();
	
	private final Texture	t1;
	private final Texture	t2;
	private final Texture	t3;
	private final TextureState ts;
	
	@SuppressWarnings("static-access")
	public ReverseCount()
	{
		super("REVERSE COUNTER");
		//Carico le texture
		ts = display.getRenderer().createTextureState();
		t1  = TextureManager.loadTexture(
									this.getClass().getClassLoader().getResource("textures/1.png"),
									MinificationFilter.Trilinear,
									MagnificationFilter.Bilinear,
									1.0f, true);
		t2  = TextureManager.loadTexture(
				this.getClass().getClassLoader().getResource("textures/2.png"),
				MinificationFilter.Trilinear,
				MagnificationFilter.Bilinear,
				1.0f, true);
		t3  = TextureManager.loadTexture(
				this.getClass().getClassLoader().getResource("textures/3.png"),
				MinificationFilter.Trilinear,
				MagnificationFilter.Bilinear,
				1.0f, true);
		
		//Creo l'oggetto
		x1=display.getWidth()/2; y1=display.getHeight()/2; x2=200; y2=200;
		panel = new Quad("PANEL",(float)x2,(float)y2);
		panel.setRenderQueueMode(renderer.QUEUE_ORTHO);
		panel.setLightCombineMode(Spatial.LightCombineMode.Off);
		panel.setLocalTranslation(x1,y1,0.0f);
		panel.updateRenderState();
		
		//Imposto il materiale per il rendering dell'oggetto.
		MaterialState ms = renderer.createMaterialState();
        ms.setAmbient(new ColorRGBA(1,1,1,0));
        ms.setDiffuse(new ColorRGBA(0,0,0,1));
        ms.setEnabled(true);
        panel.setRenderState(ms);
		
		//Applico la texture
		applyTexture(3);
		this.attachChild(panel);
        System.out.println("REVERSE COUNT CREATED");
	}
	
	/**
	 * Imposta la trasparenza dell'HUD
	 * NB: la texture deve avere una zona trasparente!!!
	 */
	private void setUpTransparency()
	{
		BlendState blendState = display.getRenderer().createBlendState();
		blendState.setBlendEnabled( true );
		blendState.setSourceFunction( BlendState.SourceFunction.SourceAlpha );
		blendState.setDestinationFunction( BlendState.DestinationFunction.OneMinusSourceAlpha );
		blendState.setTestEnabled( true );
		blendState.setTestFunction( BlendState.TestFunction.GreaterThanOrEqualTo );
		blendState.setEnabled( true );
		
		panel.setRenderState(blendState);
	}
	
	public void applyTexture(int number)
	{
		Texture t;
		
		switch(number)
		{
		case 3:{t=t3;break;}
		case 2:{t=t2;break;}
		case 1:{t=t1;break;}
		default:{t=t3;}
		}
		
		ts.setTexture(t);
		ts.getTexture().getImage().getWidth(); 
		ts.getTexture().getImage().getHeight();
		ts.setEnabled(true);
		setUpTransparency();
		panel.setRenderState(ts);
	}
	
	public void hide()
	{
		this.detachAllChildren();
		this.removeFromParent();
		this.updateRenderState();
	}
	
}